package com.rin.controller;

import com.rin.entity.Patient;
import com.rin.service.DeleteService;
import com.rin.service.InsertService;
import com.rin.service.SelectService;
import com.rin.service.UpdateService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author RinMi
 * @since 2023/6/15
 */

@RestController
@RequestMapping("/patient")
public class PatientController {

    @Resource
    private InsertService insertService;

    @Resource
    private DeleteService deleteService;

    @Resource
    private UpdateService updateService;

    @Resource
    private SelectService selectService;

    @PostMapping("insertPatient")
    public void insertPatient(@RequestBody Patient patient) {
        try {
            insertService.insert(patient);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("deletePatient")
    public void deletePatient(@RequestBody Patient patient) {
        String entityId = String.valueOf(patient.getPatientId());
        deleteService.delete(patient, entityId);
    }

    @PostMapping("deletePatientBatch")
    public void deletePatientBatch(@RequestBody List<Patient> patientList) {
        for (Patient patient : patientList) {
            String entityId = String.valueOf(patient.getPatientId());
            deleteService.delete(patient, entityId);
        }
    }

    @PostMapping("updatePatient")
    public void updatePatient(@RequestBody Patient patient) {
        String entityId = String.valueOf(patient.getPatientId());
        updateService.update(patient, entityId);
    }

    @PostMapping("selectPatient")
    public Object selectPatient(@RequestBody Patient patient) {
        try {
            return selectService.select(patient, String.valueOf(patient.getPatientId()));
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @PostMapping("selectPatientBatch")
    public List<Object> selectPatientBatch(@RequestBody Patient patient) {
        try {
            return selectService.selectBatch(patient);
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.emptyList();
        }
    }

    @PostMapping("initRepository")
    public void initRepository(@RequestBody Patient patient) {
        try {
            selectService.initRepository(patient);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
